package Controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import GUI.AddLivArTabGUI;

/**
 * The Class AddLivArTabController.
 */
public class AddLivArTabController implements Validate,Observer{

	/** The previous controller. */
	private ManageLivArTabController prev;
	
	/** The add living area table gui. */
	private AddLivArTabGUI addLivArTabGUI;
		

	
	/**
	 * Instantiates a new adds the living area table controller.
	 *
	 * @param addLivArTabGUI the add living area table gui
	 */
	AddLivArTabController(AddLivArTabGUI addLivArTabGUI) {
		this.addLivArTabGUI=addLivArTabGUI;
		ClientController.setObserver(this);
		this.addLivArTabGUI.addATTButtonActionListener(new ATTListener());
		this.addLivArTabGUI.addBackButtonActionListener(new BackListener());
		this.addLivArTabGUI.setVisible(true);
		
	}
	
	/* (non-Javadoc)
	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
	 */
	@Override
	public void update(Observable o, Object arg) {
		String msg = (String) arg.toString();
		if(msg.equals("#Server:Action succeded"))	// an update was executed
		{
			addLivArTabGUI.displayMessage("Living area added successfuly");
			LoginController.mainWindow.setContentPane(prev.getManageLivArTabGUI());
			LoginController.mainWindow.setTitle("Manage living area table");
		}
		else
		{
			addLivArTabGUI.displayMessage("Fail to add this living area");
		}
		
	}

	/* (non-Javadoc)
	 * @see Controllers.Validate#validateInput()
	 */
	@Override
	public boolean validateInput() {

		
		String area=addLivArTabGUI.getNewArea();
		if(area.equals(""))
		{
			addLivArTabGUI.displayWarningMessage("Please fill all the missing fields.");
			return false;
		}
		return true;

	}
	
	/**
	 * Sets the previous controller.
	 *
	 * @param prev the new previous controller
	 */
	public void setPrev(ManageLivArTabController prev) {
		this.prev = prev;
	}


	/**
	 * The listener interface for receiving ATT events.
	 * The class that is interested in processing a ATT
	 * event implements this interface, and the object created
	 * with that class is registered with a component using the
	 * component's <code>addATTListener<code> method. When
	 * the ATT event occurs, that object's appropriate
	 * method is invoked.
	 *
	 * @see ATTEvent
	 */
	class ATTListener implements ActionListener
	{
		
		/* (non-Javadoc)
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(validateInput())
			{
				String query;
				query="INSERT INTO livingareas (`name`) VALUE ('"+addLivArTabGUI.getNewArea()+"')";
				try {
					ClientController.observableClient.sendToServer(query);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
		}
		
	}
	
	
	/**
	 * The listener interface for receiving back events.
	 * The class that is interested in processing a back
	 * event implements this interface, and the object created
	 * with that class is registered with a component using the
	 * component's <code>addBackListener<code> method. When
	 * the back event occurs, that object's appropriate
	 * method is invoked.
	 *
	 * @see BackEvent
	 */
	class BackListener implements ActionListener
	{

		/* (non-Javadoc)
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			LoginController.mainWindow.setContentPane(prev.getManageLivArTabGUI());
			LoginController.mainWindow.setTitle("Manage living area table");
		}
		
	}

}
